<template>
    <div class="shopcar-container">
        <!-- 商品列表区域-->
        <div class="goods-list">
            <div class="mui-card"  v-for="(item , index) in getGoodsList" :key="item.id">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<mt-switch v-model="$store.getters.getGoodsSelected[item.id]" @change="changeSelected(item.id, $store.getters.getGoodsSelected[item.id])"></mt-switch>
                        <img :src="item.img">
                        <div class="info">
                            <h1>{{ item.name }}</h1>
                            <p>
                                <span class="price">￥{{ item.price }}</span>
                                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                                <!--从购物车获取商品的数量 -->
                                <!-- 1. 创建一个空对象，循环购物车中所有的商品数据，把当前循环这条商品的 id 作为对象的 key, count 作为 value { 1: 2, 2: 3} -->
                                <a  @click.prevent="remove(item.id, index)">删除 </a>
                            </p>
                        </div>
					</div>
				</div>
			</div>
        </div>
        <!-- 结算区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                   <div class="left">
                       <p>总计（不含运费）：</p>
                       <p>已勾选商品 <span class="red">{{ $store.getters.getCountAndPrice['count'] }}</span> 件 , 总价 ￥<span class="red">{{ $store.getters.getCountAndPrice['price'] }}</span></p>
                   </div>
                   <mt-button type="danger" > 去结算</mt-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import numbox from '../subcomponents/shopcar_numberBox.vue'
export default {
    data(){
        return {
            goodsList: []
        }
    },
    methods: {
        getGoodsList(){
            // 获取购物车中的 商品id
           this.getGoodsList =  this.$store.state.car
        },
        remove(id, index){ // 删除购物车中的数据, index 用于删除 goodsList 中的数据
            this.goodsList.splice(index, 1)
            this.$store.commit('removeFromCar', id)

        },
        changeSelected(id, value){ // 按钮状态改变
            this.$store.commit('changeSelected', { id: id, value: value})

        }
    },
    created(){
        this.getGoodsList();
    },
    components: {
        numbox
    }
}
</script>

<style lang="scss" scoped>
.shopcar-container{
    background-color: #eee;
    overflow: hidden;
    .goods-list{
        .mui-card-content-inner{
            display: flex;
            align-items: center;
        }
        img{
            width: 60px;
            height: 60px;
        }
        h1{
            font-size: 13px;
        }
        .info{
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .price{
                color: red;
                font-weight: bold;
            }
        }
    }
    .jiesuan{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .red{
            color: red;
            font-weight: bold;
            font-size: 16px;
        }

    }
}

</style>